package com.jichangxiu.framework.config;

import com.jichangxiu.common.properties.JcxProperties;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * SwaggerConfig:
 *
 * @author Jcx
 * @create 2023-04-04 13:03
 */
@Slf4j
@Configuration
@EnableConfigurationProperties({JcxProperties.class})
public class SwaggerConfig {

    @Resource
    private JcxProperties jcxProperties;

    @Bean
    public GroupedOpenApi userApi() {
        log.info("加载 Swagger 配置 1");
        String[] paths = {"/**"};
        String[] packagedToMatch = {jcxProperties.getSwaggerProperties().getBasePackage()};
        return GroupedOpenApi.builder().group(jcxProperties.getSwaggerProperties().getTitleAndGroup())
                .pathsToMatch(paths)
                .packagesToScan(packagedToMatch).build();
    }

    @Bean
    public OpenAPI customOpenAPI() {
        log.info("加载 Swagger 配置 2");
        JcxProperties.SwaggerProperties swaggerProperties = jcxProperties.getSwaggerProperties();
        Contact contact = new Contact();
        contact.setName(swaggerProperties.getContactName());
        contact.setEmail(swaggerProperties.getContactEmail());
        contact.setUrl(swaggerProperties.getContactUrl());
        return new OpenAPI().info(new Info()
                .title(swaggerProperties.getTitleAndGroup())
                .description(swaggerProperties.getDescription())
                .contact(contact)
                .version(swaggerProperties.getVersion())
                .termsOfService(swaggerProperties.getTermsOfServiceUrl())
                .license(new License().name(swaggerProperties.getLicense())
                        .url(swaggerProperties.getLicenseUrl())));
    }

}
